{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f7386919",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Australia', 'Austria', 'Belarus', 'Belgium', 'Bulgaria', 'Canada', 'Croatia', 'Cyprus', 'Czechia', 'Denmark', 'Estonia', 'European Union', 'Finland', 'France', 'Germany', 'Greece', 'Hungary', 'Iceland', 'Ireland', 'Italy', 'Japan', 'Latvia', 'Liechtenstein', 'Lithuania', 'Luxembourg', 'Malta', 'Monaco', 'Netherlands', 'New Zealand', 'Norway', 'Poland', 'Portugal', 'Romania', 'Russian Federation', 'Slovakia', 'Slovenia', 'Spain', 'Sweden', 'Switzerland', 'Turkey', 'Ukraine', 'United Kingdom', 'United States of America']\n"
     ]
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "from xml.dom import minidom\n",
    "import socket\n",
    "import tkinter as tk\n",
    "from tkinter import ttk\n",
    "import xml\n",
    "import threading\n",
    "import time\n",
    "import socket\n",
    "import json\n",
    "import queue\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "queue1 = queue.Queue(500)\n",
    "queue = queue.Queue(500)\n",
    "select = \"\"\n",
    "def server_start():\n",
    "    ip_port = (socket.gethostname(), 8080)\n",
    "    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)     # 创建套接字\n",
    "    s.connect(ip_port)\n",
    "    while True:\n",
    "        inp = queue.get()\n",
    "        s.sendall(inp.encode())\n",
    "        server_reply = s.recv(1024).decode()\n",
    "        server_reply = json.loads(server_reply)\n",
    "        queue1.put(server_reply)\n",
    "def plotxy(df):\n",
    "    plt.title(select)\n",
    "    df.value.plot()\n",
    "def get_data():\n",
    "    if queue1.empty():\n",
    "        print('请重新发送一遍！！！')\n",
    "    else:\n",
    "        data = queue1.get()\n",
    "        df = pd.DataFrame(data)\n",
    "        df.year = pd.to_datetime(df.year)\n",
    "        df.value = pd.to_numeric(df.value)\n",
    "        df.set_index(df.year,inplace=True)\n",
    "        t1 = threading.Thread(target=plotxy,args=(df,))\n",
    "        t1.start()\n",
    "# #open xml file\n",
    "# dom = xml.dom.minidom.parse('UNData.xml')\n",
    "\n",
    "# #get elements in the file\n",
    "# root = dom.documentElement\n",
    "\n",
    "# #search by Tag\n",
    "# cc = dom.getElementsByTagName('Country')\n",
    "\n",
    "# for c in cc:\n",
    "#     # elements of country\n",
    "#     country = []\n",
    "#     a = 0\n",
    "#     while a < len(cc):\n",
    "#         country.append(cc[a].firstChild.data)\n",
    "#         a+=28\n",
    "\n",
    "# use Tkinter , create a window\n",
    "\n",
    "t = threading.Thread(target=server_start)\n",
    "t.start()\n",
    "win = tk.Tk()\n",
    "win.title(\"Select a country\")  # Title of the window\n",
    "win.geometry(\"500x300+200+20\")\n",
    "'''\n",
    "pull-down menu\n",
    "'''\n",
    "xVariable = tk.StringVar()\n",
    "\n",
    "# Create a pull-down menu\n",
    "country=['Australia', 'Austria', 'Belarus', 'Belgium', 'Bulgaria', 'Canada', 'Croatia', 'Cyprus', 'Czechia', 'Denmark', 'Estonia', 'European Union', 'Finland', 'France', 'Germany', 'Greece', 'Hungary', 'Iceland', 'Ireland', 'Italy', 'Japan', 'Latvia', 'Liechtenstein', 'Lithuania', 'Luxembourg', 'Malta', 'Monaco', 'Netherlands', 'New Zealand', 'Norway', 'Poland', 'Portugal', 'Romania', 'Russian Federation', 'Slovakia', 'Slovenia', 'Spain', 'Sweden', 'Switzerland', 'Turkey', 'Ukraine', 'United Kingdom', 'United States of America']\n",
    "\n",
    "com = ttk.Combobox(win, textvariable=xVariable)\n",
    "com.pack()\n",
    "com[\"value\"] = country\n",
    "print(country)\n",
    "com.current(0)  # Set a default value\n",
    "com.bind(\"<<ComboboxSelected>>\")\n",
    "button1 = tk.Button(win, text=\"plotxy\",command=get_data)\n",
    "def put_info():\n",
    "    global select\n",
    "    country_select = com.get()\n",
    "    select = country_select\n",
    "    queue.put(country_select)\n",
    "#     print(country_select)\n",
    "    button1.pack()\n",
    "button = tk.Button(win, text=\"send\",command=put_info)\n",
    "button.pack()\n",
    "win.mainloop()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7e981330",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
